<?php

namespace app\user\controller;

use app\common\controller\Base;
use app\common\lib\Netease as ease;
use app\common\lib\Util;
use app\common\model\User;
use think\Request;

class NetEase extends Base
{
    protected  $request;
    public function __construct(Request $request)
    {
        parent::__construct();
        $this->request = $request;
    }

    //创建用户
    public function save(ease $ease)
    {
        $data = [
            'accid' => User::getAccid()
        ];
        $result = $ease->createUser($data);
        if ($result == false) {
            return Util::apiResponse([], '参数错误', 400);
        }
        if ($result['error'] == 1) {
            return Util::apiResponse([], $result['message'], 400);
        }
        return Util::apiResponse([], '', 200);
    }

    //获取用户信息
    public function getUinfos(ease $ease)
    {
        $accids = [$this->accid];
        $result = $ease->getUinfos($accids);
        if ($result['error'] == 1) {
            return Util::apiResponse([], $result['message'], 400);
        }
        return Util::apiResponse($result, '', 200);
    }

    //更新用户
    public function updateUinfo(ease $ease)
    {
        $region =  $this->request->param('region');
        $data = ['accid' => $this->accid, 'ex' => ['region'=>$region]];
        $result = $ease->updateUinfo($data);
        if ($result == false) {
            return Util::apiResponse([], '参数错误', 400);
        }
        if ($result['error'] == 1) {
            return Util::apiResponse([], $result['message'], 400);
        }
        return Util::apiResponse($result, '', 200);
    }

    //封禁用户
    public function blockUser(ease $ease)
    {
        $data = ['accid' => $this->accid, 'needkick' => false];
        $result = $ease->blockUser($data);
        if ($result == false) {
            return Util::apiResponse([], '参数错误', 400);
        }
        if ($result['error'] == 1) {
            return Util::apiResponse([], $result['message'], 400);
        }
        return Util::apiResponse([], '', 200);
    }

    //添加好友
    public function addFriend(Request $request,User $user,ease $ease)
    {
        $type =  intval($request->param('type'));
        $phoneNum = $request->param('phone_num');
        $faccid = $user->getAccidByPhoneNum($phoneNum);
        if(empty($faccid)){
            return Util::apiResponse([], '添加的好友不存在', 400);
        }
        $data = ['accid' => $this->accid,'faccid' =>$faccid,'type' => $type];
        $result = $ease->addFriend($data);
        if ($result == false) {
            return Util::apiResponse([], '参数错误', 400);
        }
        if ($result['error'] == 1) {
            return Util::apiResponse([], $result['message'], 400);
        }
        return Util::apiResponse([], '', 200);
    }

    public function friendList(ease $ease){
        $data = ['accid'=>$this->accid,'updatetime'=>1563847394];
        $result = $ease->friendList($data);
        if ($result == false) {
            return Util::apiResponse([], '参数错误', 400);
        }
        if ($result['error'] == 1) {
            return Util::apiResponse([], $result['message'], 400);
        }
        foreach ($result['list'] as $k=>&$v){
            $faccid = $v['faccid'];
            $info = $ease->getUinfos([$faccid]);
            if ($info['error'] == 1) {
                return Util::apiResponse([], $result['message'], 400);
            }
            $v['name'] = $info['name'];
        }
        return Util::apiResponse(['list'=>$result['list']], '', 200);
    }
}
